/* 
 *
 */
package com.cory.admin.sys.config;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import com.cory.boot.component.netty.TCPServer;

/**
 * 监听启动netty
 * @author xiongcong
 * @date 2020-01-21
 */
@Slf4j
public class NettyListener implements ApplicationListener<ApplicationReadyEvent> {
    
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        
        ConfigurableApplicationContext context = event.getApplicationContext();
        TCPServer tcp = (context.getBean(TCPServer.class));
        if (tcp != null) {
            ExecutorService executor = Executors.newCachedThreadPool();
            executor.execute(() -> {
                try {
                    tcp.start();
                } catch (Exception e) {
                    log.error("start netty error", e);
                }
            });
        }
    };

}
